package com.tmc.dao.impl.inthotel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.tmc.bean.common.Pagination;
import com.tmc.bean.inthotel.hotel.City;
import com.tmc.dao.impl.BaseDaoImpl;
import com.tmc.util.ObjectUtil;

@Repository
public class IntHotelCityDaoImpl extends BaseDaoImpl {

	public List<City> findCityByCityName(String cityName,String cityNameCN){
		Session session = getSession();
		String hql = "from City c where c.cityName = :cityName or c.cityNameCN = :cityNameCN";
		Query query = session.createQuery(hql);
		query.setString("cityName", cityName);
		query.setString("cityNameCN", cityNameCN);
		List<City> list =(List<City>)query.list();
		session.close();
		return list;
	}
	
    public List<City> findAll(){
    	Session session = getSession();
		String hql = "from City c where c.enabledFlag = 'Y' and c.switchs = 0 ";
		Query query = session.createQuery(hql);
		List<City> list =(List<City>)query.list();
		session.close();
		return list;
    }
    
    public List<City> findAllCity(){
    	Session session = getSession();
		String hql = "from City c where c.enabledFlag = 'Y' ";
		Query query = session.createQuery(hql);
		List<City> list =(List<City>)query.list();
		session.close();
		return list;
    }
    
    public void updateAllCity(List<City> cities){
    	Session session = getSession();
    	session.beginTransaction();
    	for (int i = 0; i < cities.size(); i++) {
			session.update(cities.get(i));
		}
    	session.getTransaction().commit();
    	session.close();
    }
    
    public void saveAllCity(List<City> cities){
    	Session session = getSession();
    	session.beginTransaction();
    	for (int i = 0; i < cities.size(); i++) {
			session.save(cities.get(i));
		}
    	session.getTransaction().commit();
    	session.close();
    }
    
    public List<City> findCity(City city){
    	StringBuilder sql = new StringBuilder();
    	sql.append(" from City c where c.enabledFlag = 'Y' ");
    	if(ObjectUtil.hasText(city.getCountryName())){
    	    Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    	    Matcher m = p.matcher(city.getCountryName());
    	    if(m.find()){
    			sql.append(" and c.countryNameCN =:countryName ");
    		}else{
    			sql.append(" and c.countryName =:countryName ");
    		}
    	}
    	if(ObjectUtil.hasText(city.getCityName())){
    		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(city.getCityName());
    		if(m.find()){
    			sql.append(" and c.cityNameCN =:cityName ");
    		}else{
    			sql.append(" and c.cityName =:cityName ");
    		}
    	}
    	sql.append(" order by c.cityId desc ");
    	Query query = getSession().createQuery(sql.toString());
    	if(ObjectUtil.hasText(city.getCityName())){
    		query.setString("cityName", city.getCityName());
    	}
    	if(ObjectUtil.hasText(city.getCountryName())){
    		query.setString("countryName", city.getCountryName());
    	}
    	return query.list();
    }
    
    public Pagination<City> findByCon(City city,Integer pageIndex,Integer pageSize){
    	Map<String, Object> params = new HashMap<String, Object>();
    	StringBuilder sql = new StringBuilder();
    	sql.append(" from City c where c.enabledFlag = 'Y' ");
    	if(ObjectUtil.hasText(city.getCityName())){
    		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(city.getCityName());
    		if(m.find()){
    			sql.append(" and c.cityNameCN like :cityName ");
    			params.put("cityName", "%" + city.getCityName() + "%");
    		}else{
    			sql.append(" and c.cityName like :cityName ");
    			params.put("cityName", "%" + city.getCityName() + "%");
    		}
		}
    	if(city.getCountryName()!=null){
    		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(city.getCountryName());
    		if(m.find()){
    			sql.append(" and c.countryNameCN =:countryName ");
    			params.put("countryName", city.getCountryName());
    		}else{
    			sql.append(" and c.countryName =:countryName ");
    			params.put("countryName", city.getCountryName());
    		}
    	}
    	sql.append(" order by c.cityId desc ");
    	return this.findPagination(sql.toString(),params, pageIndex, pageSize);
    }
    
}
